<template>
  <div ref="buttonGroup" class="web-office-button">

      <div>
        <el-button v-if="buttonType == 'create'" type="primary" @click.stop="toWebOfficeCreate()">
          {{buttonText}}
        </el-button>
        <el-button v-if="buttonType == 'check'" type="primary" @click.stop="toWebOfficeCheck()" >
          {{buttonText}}
        </el-button>
        <el-button  v-if="buttonType == 'signature'"
                    type="primary"
                    :loading="signatureContrl"
                   @click.stop="toCreateSignature()">{{signatureName}}
        </el-button>
      </div>

  </div>

</template>

<script>
  import * as WebCtrl from "../../assets/WebOffice";
  import {webOfficeCreateSpare, getfileinfo, toCreateSignature} from "@/api/system";
  import store from '@/store';
  import preview from "@/components/file/preview"

  export default {
    name: "webOfficeButton",
    components: {preview},
    data() {
      return {
        loadingControl: false,
        officeFileId: null,
        signaturePdfId: null,
        signatureName:'签章',
        signatureContrl:false,
      };
    },
    model: {
      prop: 'form',
      event: 'updateForm'
    },
    props: {
      form: {
        type: Object
      },
      buttonText: {
        type: String,
        required: true
      },
      type: {
        type: String,
        required: true
      },
      listTitle: {
        type: String
      },
      listCode: {
        type: String
      }
    },
    watch: {
      form: {
        handler() {
          this.init();
        },
        deep: true
      },
      officeFileId() {
        this.$emit('updateForm', Object.assign({}, this.form, {officeFileId: this.officeFileId}));
      },
      signaturePdfId() {
        this.$emit('updateForm', Object.assign({}, this.form, {pdfFileId: this.signaturePdfId}));
      },
    },
    computed: {
      buttonType: {
        get() {
          if (this.officeFileId != null && this.type == 'create') {
            return 'check'
          }
          return this.type;
        }
      }
    },
    created() {
      this.init();
    },
    methods: {
      init() {
        if (this.form != null) {
          this.officeFileId = this.form.officeFileId;
        }
      },

      toWebOfficeCreate() {
        this.webOfficeCreateSpare((fileId) => {
          this.officeFileId = fileId;
          //拟稿：create   核稿:check  签章：signature
          WebCtrl.ShowPage(store.getters.access_token, 'create', fileId, null, null, this.listTitle, this.listCode);
        });
      },
      webOfficeCreateSpare(callback) {
        this.loadingControl = true;
        webOfficeCreateSpare().then(response => {
          if (response.data.code == 0) {
            let fileId = response.data.data;
            this.loadingControl = false;
            callback(fileId);
          }
        })
      },
      toWebOfficeCheck() {
        getfileinfo(this.officeFileId).then((res) => {
          let fileInfo = res.data.data;
          if (fileInfo != null && fileInfo.size > 0) {
            WebCtrl.ShowPage(store.getters.access_token, 'check', fileInfo.fileId, fileInfo.fileName,null,this.listTitle);
          } else {
            this.toWebOfficeCreate((fileId) => {
              this.officeFileId = fileId;
            })
          }
        });
      },
      //无用的方法
      toWebOfficeSignature() {
        getfileinfo(this.officeFileId).then((res) => {
          let fileInfo = res.data.data;
          if (fileInfo != null && fileInfo.size > 0) {
            this.webOfficeCreateSpare((pdfFileId) => {
              this.pdfFileId = pdfFileId;
              WebCtrl.ShowPage(store.getters.access_token, 'signature', fileInfo.fileId, fileInfo.fileName, pdfFileId);
            });
          } else {
            this.toWebOfficeCreate((fileId) => {
              this.officeFileId = fileId;
            });
          }
        });
      },

      //新增签章接口
      toCreateSignature() {
        this.signatureContrl=true;
        this.signatureName='签章中,请稍后...';

        toCreateSignature(this.officeFileId).then(response => {
          if (response.data.code == 0) {
            this.signaturePdfId = response.data.data;
            this.signatureContrl=false;
            this.signatureName='签章';
            this.$notify({
              title: "成功",
              message: "签章成功,可在批文中查看",
              type: "success",
              duration: 2000
            });
          }
        })
      },
      clickBtn() {
        this.$refs['buttonGroup'].children[0].click();
      }
    }
  }
</script>

<style scoped>
  .web-office-button {
    margin: 0 10px;
  }
</style>
